<?xml version="1.0" encoding="UTF-8" ?>
<%@page language="java" contentType="text/xml"%>
<%@page import="java.util.*"%>
<%@page import="java.io.PrintWriter"%>
<%@page import="com.jimet.objects.*"%>
<%@page import="java.lang.reflect.Method"%>
<%@page import="com.boun.objects.*"%>
<%
	PrintWriter printwriter = new PrintWriter(out);
	Vector<String> strOut = new Vector<String>();
	request.setCharacterEncoding("UTF-8"); //$NON-NLS-1$
	response.setLocale(new java.util.Locale("tr", "TR")); //$NON-NLS-1$ //$NON-NLS-2$
	response.setCharacterEncoding("UTF-8"); //$NON-NLS-1$
	
	FilterParamsNew filterSession 	= ((FilterParamsNew) session.getAttribute("filterNew"));
	String elementType 				= request.getParameter("elementType") 		!= null ? request.getParameter("elementType"):"";
	String elementId 				= request.getParameter("elementId") 		!= null ? request.getParameter("elementId"):"";
	String sprgObjId 				= request.getParameter("sprgObjId") 		!= null ? request.getParameter("sprgObjId"):"-1";
	String elementToSelect 			= request.getParameter("elementToSelect") 	!= null ? request.getParameter("elementToSelect"):"-1";
	
	if (elementType.equals("")) 
	{
		strOut.add("<selectInput>");
		strOut.add("</selectInput>");
		for (int i = 0; i < strOut.size(); i++) 
		{
			printwriter.write(strOut.get(i));
			//System.out.println(strOut.get(i));
		}
		return;
	}
	strOut.add("<selectInput>");

	//filterSession.getClassName();
	//System.out.println(elementType);
	//System.out.println(elementId);
	//System.out.println(elementToSelect);

	Class filterClass = filterSession!=null?filterSession.getClassName():null;
	if(elementType.indexOf(".")>-1)
	{
		String[] strVals = elementType.split("\\.");
		
		Class childObjectClass = filterClass.getDeclaredField(strVals[0]).getType();
		
		String childObjectVariableName = strVals[1].substring(0,strVals[1].lastIndexOf("Id"));
		
		//System.out.println(childObjectClass.getName());
		
		TemplateUtil util = new TemplateUtil();


		//System.out.println(util.makePlural( childObjectClass.getDeclaredField(childObjectVariableName).getDeclaringClass().getName()));
		
		Class classToLoad = Class.forName(util.makePlural( childObjectClass.getDeclaredField(childObjectVariableName).getType().getName()));
		
		//System.out.println(classToLoad.getName());
		
		Method load = classToLoad.getMethod ("load",new Class[]{HashMap.class,String.class,String.class,Long.class,Long.class});
		Method getObject = classToLoad.getMethod("getObject");
		
		
		JimetObjects objects = (JimetObjects)classToLoad.newInstance();
		load.invoke(objects,new Object[]{null,null,null,null,null});

		Class objClass = (Class)getObject.invoke(objects);
		Method getId = objClass.getMethod("getId");
		Method toPrint = objClass.getMethod("getId");
		
		if(objClass.getMethod("getFullname")!=null)
			toPrint = objClass.getMethod("getFullname");
		else if(objClass.getMethod("getName")!=null)
			toPrint = objClass.getMethod("getName");
		else if(objClass.getMethod("getDesc")!=null)
			toPrint = objClass.getMethod("getDesc");
		else if(objClass.getMethod("getDescription")!=null)
			toPrint = objClass.getMethod("getDescription");
		else if(objClass.getMethod("getCode")!=null)
			toPrint = objClass.getMethod("getCode");
		else if(objClass.getMethod("getBody")!=null)
			toPrint = objClass.getMethod("getBody");		
			
		strOut.add("<optionItem optionValue=\"-1\" "
				+ ">"
				+ XMLCoder.EncodeXML(new String("Tümü"
						.getBytes("ISO-8859-9"), "UTF-8"))
				+ "</optionItem>");
		
		while( objects.next() )
		{
			JimetObject obj = (JimetObject)objects.getNext();
			
			strOut
			.add("<optionItem optionValue=\""
					+ getId.invoke(obj)
					+ "\""
					+ " "
					+ ( String.valueOf(getId.invoke(obj)).equals(elementToSelect) ? " selected=\"selected\""
							: "")
					+ " onclick=\""
					+ "\">"
					+ XMLCoder.EncodeXML(String
							.valueOf(toPrint.invoke(obj)))
					+ "</optionItem>");			
			
//			System.out.println(toPrint.invoke(obj));
		}
		
	}
	else
	{
		if ( elementType.equals("status") )
		{
			if ( filterSession.getClassName() == PageContent.class )
			{
				strOut.add("<optionItem optionValue=\"-1\" "
						+ ">"
						+ XMLCoder.EncodeXML(new String("Tümü".getBytes("ISO-8859-9"), "UTF-8"))
						+ "</optionItem>");		
				strOut.add("<optionItem optionValue=\"1\">"
						+ XMLCoder.EncodeXML(new String("Aktif".getBytes("ISO-8859-9"), "UTF-8")) 
						+ "</optionItem>");		
				strOut.add("<optionItem optionValue=\"0\">"
						+ XMLCoder.EncodeXML(new String("Pasif".getBytes("ISO-8859-9"), "UTF-8")) 
						+ "</optionItem>");							
			}
			else if ( filterSession.getClassName() == SVContactItem.class )
			{
				strOut.add("<optionItem optionValue=\"-1\" "
						+ ">"
						+ XMLCoder.EncodeXML(new String("Tümü".getBytes("ISO-8859-9"), "UTF-8"))
						+ "</optionItem>");	
				strOut.add("<optionItem optionValue=\"2\">"
						+ XMLCoder.EncodeXML(new String("cevaplandı".getBytes("ISO-8859-9"), "UTF-8")) 
						+ "</optionItem>");		
				strOut.add("<optionItem optionValue=\"1\">"
						+ XMLCoder.EncodeXML(new String("okundu".getBytes("ISO-8859-9"), "UTF-8")) 
						+ "</optionItem>");		
				strOut.add("<optionItem optionValue=\"0\">"
						+ XMLCoder.EncodeXML(new String("yeni".getBytes("ISO-8859-9"), "UTF-8")) 
						+ "</optionItem>");							
			}
			else if ( filterSession.getClassName() == SchoolAppeal.class )
			{
				strOut.add("<optionItem optionValue=\"-1\" "
						+ ">"
						+ XMLCoder.EncodeXML(new String("Tümü".getBytes("ISO-8859-9"), "UTF-8"))
						+ "</optionItem>");	
				strOut.add("<optionItem optionValue=\"2\">"
						+ XMLCoder.EncodeXML(new String("reddedildi".getBytes("ISO-8859-9"), "UTF-8")) 
						+ "</optionItem>");		
				strOut.add("<optionItem optionValue=\"1\">"
						+ XMLCoder.EncodeXML(new String("onaylandı".getBytes("ISO-8859-9"), "UTF-8")) 
						+ "</optionItem>");		
				strOut.add("<optionItem optionValue=\"0\">"
						+ XMLCoder.EncodeXML(new String("onay bekliyor".getBytes("ISO-8859-9"), "UTF-8")) 
						+ "</optionItem>");							
			}
			else if ( filterSession.getClassName() == PersonalAppeal.class )
			{
				strOut.add("<optionItem optionValue=\"-1\" "
						+ ">"
						+ XMLCoder.EncodeXML(new String("Tümü".getBytes("ISO-8859-9"), "UTF-8"))
						+ "</optionItem>");	
				strOut.add("<optionItem optionValue=\"2\">"
						+ XMLCoder.EncodeXML(new String("reddedildi".getBytes("ISO-8859-9"), "UTF-8")) 
						+ "</optionItem>");		
				strOut.add("<optionItem optionValue=\"1\">"
						+ XMLCoder.EncodeXML(new String("onaylandı".getBytes("ISO-8859-9"), "UTF-8")) 
						+ "</optionItem>");		
				strOut.add("<optionItem optionValue=\"0\">"
						+ XMLCoder.EncodeXML(new String("onay bekliyor".getBytes("ISO-8859-9"), "UTF-8")) 
						+ "</optionItem>");							
			}
			else if ( filterSession.getClassName() == ContactQuestion.class )
			{
				strOut.add("<optionItem optionValue=\"-1\" "
						+ ">"
						+ XMLCoder.EncodeXML(new String("Tümü".getBytes("ISO-8859-9"), "UTF-8"))
						+ "</optionItem>");	
				strOut.add("<optionItem optionValue=\"2\">"
						+ XMLCoder.EncodeXML(new String("cevaplandı".getBytes("ISO-8859-9"), "UTF-8")) 
						+ "</optionItem>");		
				strOut.add("<optionItem optionValue=\"1\">"
						+ XMLCoder.EncodeXML(new String("okundu".getBytes("ISO-8859-9"), "UTF-8")) 
						+ "</optionItem>");		
				strOut.add("<optionItem optionValue=\"0\">"
						+ XMLCoder.EncodeXML(new String("yeni".getBytes("ISO-8859-9"), "UTF-8")) 
						+ "</optionItem>");							
			}
			
			
			
		}
		else if ( elementType.equals("type") )
		{


		}
		
		else
		{
		
			String childObjectVariableName = elementType.substring(0,elementType.lastIndexOf("Id"));
			Class childObjectClass = filterClass.getDeclaredField(childObjectVariableName).getType();
			
			//System.out.println(childObjectClass.getName());
			
			TemplateUtil util = new TemplateUtil();

			
			Class classToLoad = Class.forName(util.makePlural( childObjectClass.getName()));
			
			//System.out.println(classToLoad.getName());
			
			Method load = classToLoad.getMethod ("load",new Class[]{HashMap.class,String.class,String.class,Long.class,Long.class});
			Method getObject = classToLoad.getMethod("getObject");
			
			
			JimetObjects objects = (JimetObjects)classToLoad.newInstance();
			load.invoke(objects,new Object[]{null,null,null,null,null});
	
			Class objClass = (Class)getObject.invoke(objects);
			Method getId = objClass.getMethod("getId");
			Method toPrint = objClass.getMethod("getId");
			
			boolean hasFuncName	= false;
			try {
				if( !hasFuncName && objClass.getMethod("getFullname")!=null )
				{
					toPrint 	= objClass.getMethod("getFullname");
					hasFuncName	= true;
				}
			}
			catch ( NoSuchMethodException nex )
			{}

			try {
				if( !hasFuncName && objClass.getMethod("getName")!=null)
				{
					toPrint = objClass.getMethod("getName");
					hasFuncName	= true;
				}
			}
			catch ( NoSuchMethodException nex )
			{}
			
			try {
				if( !hasFuncName && objClass.getMethod("getBody")!=null)
				{
					toPrint = objClass.getMethod("getBody");
					hasFuncName	= true;
				}
			}
			catch ( NoSuchMethodException nex )
			{}
			
			
			
			try {
				if( !hasFuncName && objClass.getMethod("getDesc")!=null)
				{
					toPrint = objClass.getMethod("getDesc");
					hasFuncName	= true;
				}
			}
			catch ( NoSuchMethodException nex )
			{}
			
			try {
				if( !hasFuncName && objClass.getMethod("getDescription")!=null)
				{
					toPrint = objClass.getMethod("getDescription");
					hasFuncName	= true;
				}
			}
			catch ( NoSuchMethodException nex )
			{}

			try {
				if( !hasFuncName && objClass.getMethod("getCode")!=null)
				{
					toPrint = objClass.getMethod("getCode");
					hasFuncName	= true;
				}
			}
			catch ( NoSuchMethodException nex )
			{}
						
				
			strOut.add("<optionItem optionValue=\"-1\" "
					+ ">"
					+ XMLCoder.EncodeXML(new String("Tümü"
							.getBytes("ISO-8859-9"), "UTF-8"))
					+ "</optionItem>");
			
			while( objects.next() )
			{
				JimetObject obj = (JimetObject)objects.getNext();
				
				strOut
				.add("<optionItem optionValue=\""
						+ getId.invoke(obj)
						+ "\""
						+ " "
						+ ( String.valueOf(getId.invoke(obj)).equals(elementToSelect) ? " selected=\"selected\""
								: "")
						+ " onclick=\""
						+ "\">"
						+ XMLCoder.EncodeXML(String
								.valueOf(toPrint.invoke(obj)))
						+ "</optionItem>");			
				
	//			System.out.println(toPrint.invoke(obj));
			}
		}
			
	}
	
	
	strOut.add("</selectInput>");

	for (int i = 0; i < strOut.size(); i++) {
		printwriter.write((String) strOut.get(i));
		//System.out.println((String)strOut.get(i));
	}
%>